/*** This do-file creates a figure comparing the changes in average daily spending
for each month in the Affinity Solutions credit and debit card data to the
Advance Monthly Retail Trade Survey (MARTS).
***/

*-------------------------------------------------------------------------------
* Set up
*-------------------------------------------------------------------------------

* Set $root
project figstabs, root
if (r(buildrunning)==0) include "${root}/code/config_interactive.do"

* Set globals
project, uses("${root}/code/set_globals.do")
include "${root}/code/set_globals.do"
local category "Spending"

* Create required subfolders
cap mkdir "${root}/results/Spending"
cap mkdir "${root}/results/paper numbers"
cap mkdir "${root}/results/paper numbers/`category'"

*-------------------------------------------------------------------------------
* Calculate Annualized Credit Card Spending
*-------------------------------------------------------------------------------

* Load NIPA Table 2.3.5 Personal Consumption Expenditures by Major Type of Product
project, uses("${root}/data/derived/NIPA/NIPA Table 2.3.5M Personal Consumption Expenditures by Major Type of Product.dta")
use "${root}/data/derived/NIPA/NIPA Table 2.3.5M Personal Consumption Expenditures by Major Type of Product.dta", clear

* Restrict to spending categories that appear on credit cards
gen in_affinity = 0
replace in_affinity = 1 if serieslabel == "Furnishings and durable household equipment"
replace in_affinity = 1 if serieslabel == "Recreational goods and vehicles"
replace in_affinity = 1 if serieslabel == "Other durable goods"
replace in_affinity = 1 if serieslabel == "Food and beverages purchased for off-premises consumption"
replace in_affinity = 1 if serieslabel == "Clothing and footwear"
replace in_affinity = 1 if serieslabel == "Gasoline and other energy goods"
replace in_affinity = 1 if serieslabel == "Other nondurable goods"
replace in_affinity = 1 if serieslabel == "Transportation services"
replace in_affinity = 1 if serieslabel == "Recreation services"
replace in_affinity = 1 if serieslabel == "Food services and accommodations"
replace in_affinity = 1 if serieslabel == "Financial services and insurance"
replace in_affinity = 1 if serieslabel == "Other services"
keep if in_affinity == 1

* Collapse all categories
gisid seriescode year month
gcollapse (sum) value, by(year month)
label var value "Nominal Annualized Card Spending, in Millions"

* Output
sort year month
gisid year month

*-------------------------------------------------------------------------------
* Load daily credit card spending in Jan 2020
*-------------------------------------------------------------------------------

* Load annualized credit card spending in January 2020
keep if year == 2020 & month == 1
assert _N == 1

* Calculate daily credit card spending in January 2020
scalar nipa_daily_spend = value[1] / 365 * 1e6  // converting annualized spending in millions to daily spending in dollars
di "Daily spending in January 2020: " (nipa_daily_spend)

*-------------------------------------------------------------------------------
* Prepare data for plot
*-------------------------------------------------------------------------------

* Load posted Affinity data
project, uses("${root}/data/web/data/Affinity - National - Daily.csv")
import delimited using "${root}/data/web/data/Affinity - National - Daily.csv", clear

keep year month day spend_all spend_all_q1 spend_all_q2 spend_all_q3 spend_all_q4
order year month day spend_all spend_all_q1 spend_all_q2 spend_all_q3 spend_all_q4

gen date = mdy(month, day, year)
format date %td
order date

* Reshape long on quartile
rename spend_all spend_all_q0
greshape long spend_all_q, i(date) j(income_q)
replace income_q = . if income_q == 0
gisid date income_q, mi

* Keep values from 2020 onward
keep if inrange(date, mdy(1,1,2020), mdy(12,31,2021))

* Merge income quartile shares
rename income_q income_quartile

* Load income shares file in Stata format
preserve
	project, uses("${root}/data/web/data/Affinity Income Shares - National - 2020.csv")
	import delimited "${root}/data/web/data/Affinity Income Shares - National - 2020.csv", clear

	keep income_quartile share_jan2020
	rename share_jan2020 share

	tempfile income_shares
	save `income_shares'
restore

merge m:1 income_quartile using `income_shares', assert(1 3) nogen
rename income_quartile income_q
replace share = 1 if income_q == .

* Change in spending since Jan 2020
gen change_since_jan_2020 = spend_all_q * share * nipa_daily_spend / 1E9

* Calculate each quartile's share of aggregate decline
gegen change_sum = sum(change_since_jan_2020) if !mi(income_q), by(date)
gen share_decline = change_since_jan_2020 / change_sum

* Mean in initial period
gen initial_spending = 1 * share * nipa_daily_spend / 1E9

* Shifted change
gen rescaled_change_since_jan_2020 = change_since_jan_2020 + initial_spending

*-------------------------------------------------------------------------------
* Plot diff-in-diff by income quartiles
*-------------------------------------------------------------------------------

* Compute last date
if !mi(${finaldate}) local last_date = ${finaldate}
else {
	sum date, meanonly
	local last_date = r(max)
}

* Obtain text values
local early = mdy(4, 14,2020)
local middle = mdy(8, 14, 2020)
local late = `last_date'

foreach period in "early" "middle" "late" {
	foreach q in 1 4 {
		sum change_since_jan_2020 if date == ``period'' & income_q == `q'
		assert r(N) == 1

		local change_`period'_sign_q`q' = cond(r(mean) < 0, "–", "+")
		local change_`period'_q`q' = string(abs(r(mean)), "%9.1f")
		local change_`period'_text_q`q' "`change_`period'_sign_q`q''$`change_`period'_q`q''"

		sum share_decline if date == ``period'' & income_q == `q'
		assert r(N) == 1

		local sharedecline_`period'_q`q' = string(r(mean) * 100, "%9.0f")
	}
}

* Get level of spending in February and Mar 25 - Apr 14 (for text)
foreach q in 1 4 {
    sum rescaled_change_since_jan_2020 if income_q == `q' & inrange(date, mdy(2, 1, 2020), mdy(2, 28, 2020))
	local spend_level_q`q'_feb2020 = `=round(`r(mean)', 0.1)'
	di `spend_level_q`q'_feb2020'


	sum rescaled_change_since_jan_2020 if income_q == `q' & inrange(date, mdy(3, 25, 2020), mdy(4, 14, 2020))
	local spend_level_q`q'_mar2020 = `=round(`r(mean)', 0.1)'
	di `spend_level_q`q'_mar2020'

	local spend_decline_febtomar2020_q`q': di %2.0f abs((`spend_level_q`q'_mar2020'/`spend_level_q`q'_feb2020') - 1) * 100

}

* Get gap between high-income spending in August 14th 2020 and 2020 levels
sum initial_spending if income_q == 4
local level_2020_q4 = `r(mean)'

sum rescaled_change_since_jan_2020 if income_q == 4 & date == mdy(8, 14, 2020)
local spend_level_aug2020_q4 = `r(mean)'

local gap_q4_spend_aug2020: di %2.0f abs((`spend_level_aug2020_q4'/`level_2020_q4') - 1) * 100
di `gap_q4_spend_aug2020'

* Obtain x-axis labels
project, uses("${root}/code/ado/xlab_months.ado") raw
xlab_months, startmonth(Jan 2020) endmonth(Nov 2021) space(2)

* Plot diff-in-diff by income quartile
tw	(line rescaled_change_since_jan_2020 date if income_q == 4 & year(date) >= 2020,  lcolor(oi2) sort) ///
	(line initial_spending date if income_q == 4 & year(date) >= 2020,  lcolor(oi2%50) sort) ///
	(line rescaled_change_since_jan_2020 date if income_q == 1 & year(date) >= 2020, lcolor(oi1) sort) ///
	(line initial_spending date if income_q == 1 & year(date) >= 2020, lcolor(oi1%50) sort) ///
	if date <= `last_date' ///
	, ///
	xtitle("") ///
	ytitle("Credit and Debit Card Spending" "Per Day ($ Billions)") ///
	legend(off) ///
	ylab(2 "$2B" 4 "$4B" 6 "$6B" 8 "$8B" 10 "$10B", nogrid) ///
	xlab( `r(xlab_months)', labsize(small)) ///
	text(9.5 `=mdy(5, 1, 2021)' "Top Income Quartile", color(oi2) size(3)) ///
	text(3 `=mdy(5, 1, 2021)' "Bottom Income Quartile", color(oi1) size(3)) ///
	///
	xline(`=mdy(4, 14, 2020)', lc(gs12) lpattern(dash)) ///
	text(10.4 `=mdy(4, 14, 2020)' "April 14 2020", color(gs7) size(2.4)) ///
	text(2.4 `=mdy(4, 14, 2020)+2' "`change_early_text_q1' Billion", place(3) color(oi1) size(2.4)) ///
	text(5 `=mdy(4, 14, 2020)+2' "`change_early_text_q4' Billion", place(3) color(oi2) size(2.4)) ///
	text(2.1 `=mdy(4, 14, 2020)+2' "(`sharedecline_early_q1'% of Agg. Decline)", place(3) justification(left) color(oi1) size(2.2)) ///
	text(4.7 `=mdy(4, 14, 2020)+2' "(`sharedecline_early_q4'% of Agg. Decline)", place(3) justification(left) color(oi2) size(2.2)) ///
	xline(`=mdy(8, 14, 2020)', lc(gs12) lpattern(dash)) ///
	text(10.4 `=mdy(8, 14, 2020)' "August 14 2020", color(gs7) size(2.4)) ///
	text(4.0 `=mdy(8, 14, 2020)' "`change_middle_text_q1' Billion", place(3) color(oi1) size(2.4)) ///
	text(7.0 `=mdy(8, 14, 2020)' "`change_middle_text_q4' Billion", place(3) color(oi2) size(2.4)) ///
	xline(`last_date', lc(gs12) lpattern(dash)) ///
	text(10.4 `=`last_date'+15' "December 31 2021", place(9) color(gs7) size(2.4)) ///
	text(4.6 `last_date' "`change_late_text_q1' Billion", place(9) color(oi1) size(2.4)) ///
	text(9.5 `last_date' "`change_late_text_q4' Billion", place(9) color(oi2) size(2.4)) ///
	xsize(10)

graph export "${root}/results/Spending/Spending Changes by Income Quartile.svg", replace
project, creates("${root}/results/Spending/Spending Changes by Income Quartile.svg")

* Black and white version for QJE
xlab_months, startmonth(Jan 2020) endmonth(Nov 2021) space(2)

tw	(line rescaled_change_since_jan_2020 date if income_q == 4 & year(date) >= 2020,  lcolor(gs8) sort) ///
	(line initial_spending date if income_q == 4 & year(date) >= 2020,  lcolor(gs8%50) sort) ///
	(line rescaled_change_since_jan_2020 date if income_q == 1 & year(date) >= 2020, lcolor(gs0) sort) ///
	(line initial_spending date if income_q == 1 & year(date) >= 2020, lcolor(gs0%50) sort) ///
	if date <= `last_date' ///
	, ///
	xtitle("") ///
	ytitle("Credit and Debit Card Spending" "Per Day ($ Billions)") ///
	legend(off) ///
	ylab(2 "$2B" 4 "$4B" 6 "$6B" 8 "$8B" 10 "$10B", nogrid) ///
	xlab( `r(xlab_months)', labsize(small)) ///
	text(9.5 `=mdy(5, 1, 2021)' "Top Income Quartile", color(gs8) size(3)) ///
	text(3 `=mdy(5, 1, 2021)' "Bottom Income Quartile", color(gs0) size(3)) ///
	///
	xline(`=mdy(4, 14, 2020)', lc(gs12) lpattern(dash)) ///
	text(10.4 `=mdy(4, 14, 2020)' "April 14 2020", color(gs7) size(2.4)) ///
	text(2.4 `=mdy(4, 14, 2020)+2' "`change_early_text_q1' Billion", place(3) color(gs0) size(2.4)) ///
	text(5 `=mdy(4, 14, 2020)+2' "`change_early_text_q4' Billion", place(3) color(gs8) size(2.4)) ///
	text(2.1 `=mdy(4, 14, 2020)+2' "(`sharedecline_early_q1'% of Agg. Decline)", place(3) justification(left) color(gs0) size(2.2)) ///
	text(4.7 `=mdy(4, 14, 2020)+2' "(`sharedecline_early_q4'% of Agg. Decline)", place(3) justification(left) color(gs8) size(2.2)) ///
	xline(`=mdy(8, 14, 2020)', lc(gs12) lpattern(dash)) ///
	text(10.4 `=mdy(8, 14, 2020)' "August 14 2020", color(gs7) size(2.4)) ///
	text(4.0 `=mdy(8, 14, 2020)' "`change_middle_text_q1' Billion", place(3) color(gs0) size(2.4)) ///
	text(7.0 `=mdy(8, 14, 2020)' "`change_middle_text_q4' Billion", place(3) color(gs8) size(2.4)) ///
	xline(`last_date', lc(gs12) lpattern(dash)) ///
	text(10.4 `=`last_date'+15' "December 31 2021", place(9) color(gs7) size(2.4)) ///
	text(4.6 `last_date' "`change_late_text_q1' Billion", place(9) color(gs0) size(2.4)) ///
	text(9.5 `last_date' "`change_late_text_q4' Billion", place(9) color(gs8) size(2.4)) ///
	xsize(10)

graph export "${root}/results/QJE_Figures_BlackAndWhite/Figure_1a.svg", replace
project, creates("${root}/results/QJE_Figures_BlackAndWhite/Figure_1a.svg")

*-------------------------------------------------------------------------------
* Export output numbers to csv file
*-------------------------------------------------------------------------------

cap erase "${root}/results/paper numbers/`category'/Spending Changes by Income Quartile.yaml"

foreach period in "early" "middle" "late" {
	foreach q in 1 4 {
		yamlout using "${root}/results/paper numbers/`category'/Spending Changes by Income Quartile.yaml", ///
			key("decline_`period'_q`q'") ///
			comment("Decline in Card Spending, in $ Billions: Income Q`q', `: di %td ``period''' ") ///
			value("`change_`period'_text_q`q''")
	}
}

foreach period in "early" {
	foreach q in 1 4 {
		yamlout using "${root}/results/paper numbers/`category'/Spending Changes by Income Quartile.yaml", ///
			key("share_decline_`period'_q`q'") ///
			comment("Decline in Card Spending, in $ Billions: Income Q`q', `: di %td ``period''' ") ///
			value("`sharedecline_`period'_q`q''")

		yamlout using "${root}/results/paper numbers/`category'/Spending Changes by Income Quartile.yaml", ///
			key("spend_level_q`q'_feb2020") ///
			comment("Spending level in billions of dollars for Q`q' in Feb 2020") ///
			value("`spend_level_q`q'_feb2020'")

		yamlout using "${root}/results/paper numbers/`category'/Spending Changes by Income Quartile.yaml", ///
					key("spend_level_q`q'_mar2020") ///
					comment("Spending level in billions of dollars for Q`q' in end of March 2020") ///
					value("`spend_level_q`q'_mar2020'")

		yamlout using "${root}/results/paper numbers/`category'/Spending Changes by Income Quartile.yaml", ///
					key("spend_decline_febtomar2020_q`q'") ///
					comment("Spending decline from Feb to Mar 2020 in Q`q'") ///
					value("`spend_decline_febtomar2020_q`q''")

	}
}

yamlout using "${root}/results/paper numbers/`category'/Spending Changes by Income Quartile.yaml", ///
					key("gap_q4_spend_aug2020") ///
					comment("High Income Spending gap relative to 2020 levels - August 2020") ///
					value("`gap_q4_spend_aug2020'")

project, creates("${root}/results/paper numbers/`category'/Spending Changes by Income Quartile.yaml")
